﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Twi.COMMON.Core;
using Twi.Pages.SCS.Model;

namespace Twi.Pages.SCS.BLL
{
    public class _OutRecord
    {
        private static readonly _OutRecord instance = new _OutRecord();
        private _OutRecord() { }
        public static _OutRecord Instance
        {
            get { return instance; }
        }
        /// <summary>
        /// 新增记录
        /// </summary>
        /// <param name="dy"></param>
        public void NewRecord(TOutRecord outRecord,List<TOutRecordDetail> listOutRecordDetails)
        {
            using (ScsDataClassesDataContext db = new ScsDataClassesDataContext(ScsConntions.TyScsConnectionString))
            {
                db.TOutRecord.InsertOnSubmit(outRecord);
                db.TOutRecordDetail.InsertAllOnSubmit(listOutRecordDetails);
                db.SubmitChanges();
            }
        }

        /// <summary>
        /// 条件查询
        /// </summary>
        /// <param name="paging"></param>
        /// <param name="multiCondition"></param>
        /// <param name="totalCount">分页前总记录数</param>
        /// <returns></returns>
        public List<TOutRecord> GetByCondition(PagingCondition paging, MultiCondition multiCondition,ref int totalCount)
        {
            using(ScsDataClassesDataContext db = new ScsDataClassesDataContext(ScsConntions.TyScsConnectionString))
            {
                var q = from p in db.TOutRecord                        
                        select p;
                if (multiCondition.BeginDate != DateTime.MinValue)
                {
                    q = q.Where(c => Convert.ToDateTime(c.FEndDate) >= multiCondition.BeginDate);
                }
                if (multiCondition.EndDate != DateTime.MaxValue)
                {
                    q = q.Where(c=>Convert.ToDateTime(c.FStartDate)<=multiCondition.EndDate);
                }
                var dict = multiCondition.Dict;
                if (dict != null)
                {
                    foreach (var key in dict.Keys)
                    {
                        if (!string.IsNullOrEmpty(dict[key]))
                        {
                            if (key.Equals("FUserRealName"))
                            {
                                q = q.Where(c => c.FUserRealNames.Contains(dict[key]));
                            }
                            else if (key.Equals("FTypeID"))
                            {                               
                                //q = q.Where(c => c.FTypeID.Equals(dict[key]));//会报错：无法识别的GUID格式，难道是引用类型的问题？
                                string str = ""+ dict[key];
                                q = q.Where(c => c.FTypeID.Equals(str));
                            }
                            
                        }
                    }
                }
                q = q.OrderByDescending(c => c.FStartDate);
                //分页前总记录数
                totalCount = q.Count();
                List<TOutRecord> list = new List<TOutRecord>();
                if (paging.needPaging)
                {
                    list = (q.Skip(paging.startIndex).Take(paging.pageSize)).ToList();
                }
                else
                {
                    list = q.ToList();
                }
                return list;
            }
        }

        /// <summary>
        /// 删除记录
        /// </summary>
        /// <param name="outRecordId"></param>
        public void Delete(string outRecordId)
        {
            using (ScsDataClassesDataContext db = new ScsDataClassesDataContext(ScsConntions.TyScsConnectionString))
            {
                var q = from p in db.TOutRecord
                        where p.FID.Equals(outRecordId)
                        select p;
                db.TOutRecord.DeleteAllOnSubmit(q);
                var q2  = from p in db.TOutRecordDetail
                          where p.FOutRecordID.Equals(outRecordId)
                          select p;
                db.TOutRecordDetail.DeleteAllOnSubmit(q2);
                db.SubmitChanges();
            }
        }

        /// <summary>
        /// 根据ID获取实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public TOutRecord GetEntityByID(string id)
        {
            using (ScsDataClassesDataContext db = new ScsDataClassesDataContext(ScsConntions.TyScsConnectionString))
            {
                var q = db.TOutRecord.First(c=>c.FID.Equals(id));
                return q;
            }
        }

        /// <summary>
        /// 编辑记录单详情
        /// </summary>
        /// <param name="id"></param>
        /// <param name="newDetail"></param>
        public void EditDetail(string id, string newDetail)
        {
            using (ScsDataClassesDataContext db = new ScsDataClassesDataContext(ScsConntions.TyScsConnectionString))
            {
                var q = db.TOutRecord.First(c => c.FID.Equals(id));
                q.FDetail = newDetail;
                db.SubmitChanges();
            }
        }
    }
}
